﻿using UnityEngine;

public class Item : TriggerArea
{
    protected SpriteRenderer m_spriteRenderer;
    protected EventSystem eventSystem;

    [SerializeField]
    protected bool _Picked;
    public bool Picked
    {
        get => _Picked;
        protected set
        {
            if (m_spriteRenderer != null)
                m_spriteRenderer.enabled = !value;
            _Picked = value;
        }
    }

    protected void Awake()
    {
        eventSystem = Service.Get<EventSystem>();
        m_spriteRenderer = GetComponent<SpriteRenderer>();
    }

    protected virtual void OnEnable()
    {
        eventSystem.PlayerDie += OnPlayerDie;
    }
    protected virtual void OnDisable()
    {
        eventSystem.PlayerDie -= OnPlayerDie;
    }
    protected virtual void OnPlayerDie()
    {
        Picked = false;
    }

    protected sealed override bool IsTarget(Collider2D collider)
    {
        return base.IsTarget(collider);
    }

    protected override void OnEnter(Collider2D collider)
    {
        Character player = collider.GetComponent<Character>();
        if (player != null && !Picked)
        {
            Picked = true;
            OnPicked(player);
        }
    }

    /// <summary>
    /// 被player拾取后自动执行
    /// </summary>
    protected virtual void OnPicked(Character player) { }
}
